﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using ViewModel;
using Image=System.Windows.Controls.Image;

namespace View
{
	/// <summary>
	/// Logique d’interaction pour WindowChangeAvatar.xaml
	/// </summary>
	public partial class WindowChangeAvatar : Window
	{
        private string _imgPath;
	    private ImageSource _imgSrc;
        private ViewModel.AvatarViewModel viewModel;
        private ViewModel.MainViewModel mainViewModel;

		public WindowChangeAvatar(MainViewModel vm)
		{
		    this.mainViewModel = vm;
			this.InitializeComponent();
		    viewModel = new ViewModel.AvatarViewModel();
		    DataContext = viewModel;
		    this.imageAvatar.Source = mainViewModel.Img;
		}

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void buttonUploadAvatar_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "Format Images (*.jpg;*.jpeg;*.png;*.bmp;*.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif|Tous les fichier (*.*)|*.*";
            bool? retval = dlg.ShowDialog();
            //int maxSize = 150000;
            if (retval != null && retval == true)
            {
                this._imgPath = dlg.FileName;
                _imgSrc = GestionImage.Preview(new Uri(dlg.FileName)); 
                //FileInfo fileInfo = new FileInfo(dlg.FileName);
                    try
                    {
                        imageNewAvatar.Stretch = Stretch.Uniform;
                        imageNewAvatar.StretchDirection = StretchDirection.Both;
                        imageNewAvatar.Source = _imgSrc;
    
                        textBlockNewSelectedAvatar.Text = dlg.SafeFileName;
                        buttonApplyNewAvatar.IsEnabled = true;
                    }
                    catch (NotSupportedException ex)
                    {
                        buttonApplyNewAvatar.IsEnabled = false;
                        MessageBox.Show(ex.Message);
                        textBlockNewSelectedAvatar.Text = "Aucun fichier valide selectionné...";
                    }
            }
            else
            {
                buttonApplyNewAvatar.IsEnabled = false;
                textBlockNewSelectedAvatar.Text = "Aucun fichier selectionné...";
            }
        }

        private void buttonApplyNewAvatar_Click(object sender, RoutedEventArgs e)
        {
                //Récupération dans la base de donnée ou pas 

            //envoyer le resultat ci dessous pour enregistrer l'avatar dans la bdd

            System.Drawing.Image img=  GestionImage.ResizeImage(new System.Drawing.Bitmap(this._imgPath));
            this.viewModel.UploadAvatar(img);
            this.mainViewModel.Img = _imgSrc;
            //Window1.imageAvatar.Source = _imgSrc;
            Close();
        }
	}
}